package it.java.giw.solr;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

/**
 * mette tutti i file dentro un buffer condiviso 
 * crea vari thread il quale ognuno prende un file e lo indicizza 
 * @author roberto
 *
 */
public class SplitFS {	
	private File root;
	private BlockingQueue<String> queue;
	private ExecutorService es;
	private static final int nCpu = Runtime.getRuntime().availableProcessors();
	
	private static final String solrUrl = "http://localhost:8983/solr/";
	
	public SplitFS (String args[]){
		this.root = new File(args[0]);
		this.queue = new LinkedBlockingQueue<String>();
		this.es = Executors.newFixedThreadPool(nCpu);
	}

	public void run() throws SolrServerException, IOException{
		long startTime = System.currentTimeMillis();
		CountDownLatch doneSignal = new CountDownLatch(nCpu);
		populateQueue();
		for (int i = 0; i<nCpu; i++){
			es.submit(new IndexingTask(queue, doneSignal));
		}
		
		try {
			doneSignal.await();
			HttpSolrServer server = new HttpSolrServer(solrUrl);
			info("COMMITting Solr index changes to " + solrUrl + "..");
			server.commit();
			server.optimize();
			es.shutdown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		displayTiming(endTime - startTime);
	}
	
	private void displayTiming(long millis)	{
		SimpleDateFormat df = new SimpleDateFormat("H:mm:ss.SSS", Locale.getDefault());
		df.setTimeZone(TimeZone.getTimeZone("UTC"));
		System.out.println("Time spent: " + df.format(new Date(millis)));
	}

	private void populateQueue() {
		if (root.isDirectory() && root.canRead()){
			expandDirectory(root);
		}
		else
			queue.add(root.getAbsolutePath());
	}

	private void expandDirectory(File dir) {
		if ((dir.isHidden()) && (!dir.getName().equals("."))) {
			return;
		}
		for (File f : dir.listFiles()) {
			if (f.isDirectory())
				expandDirectory(f);
			else
				queue.add(f.getAbsolutePath());
		}
	}


//	public static void main(String args[]) throws SolrServerException, IOException{
//		String[] arg = new String[1];
//		arg[0] = "C:/Users/Roberto/Downloads/docs";
//
//		SplitFS splitter = new SplitFS(arg);
//		
//		splitter.run();
//		Iterator<String> it = splitter.getQueue().iterator();
//		while (it.hasNext()){
//			System.out.println(it.next());
//		}
//	}

//	private BlockingQueue<String> getQueue() {
//		return this.queue;
//	}
	
	static void info(String msg)	{
		System.out.println(msg);
	}

}
